<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
	/**
	 * Authenticates a user.
	 * The example implementation makes sure if the username and password
	 * are both 'demo'.
	 * In practical applications, this should be changed to authenticate
	 * against some persistent user identity storage (e.g. database).
	 * @return boolean whether authentication succeeds.
	 */
	private $_id;
	private $_last_login_time;
	
	public function authenticate()
	{
		$this->username = str_replace("：",":",$this->username);
		$cmpSub = explode(':',$this->username);
		if(isset($cmpSub[1])){
			//商家子账号登录
			$cmpUserid = Member::model()->findByAttributes(array('zm_name' => $cmpSub[0]));
			if ($cmpUserid === null) {
				$this->errorCode = self::ERROR_USERNAME_INVALID;
			}else{
				$criteria = new CDbCriteria;
				$criteria->compare('zm_id',$cmpUserid->zm_id);
				$criteria->compare('zms_username',$cmpSub[1]);
				$memSub = MemSub::model()->find($criteria);
				if ($memSub === null) {
					$this->errorCode = self::ERROR_USERNAME_INVALID;
				}else{
					if(!$memSub->checkPassword($this->password)){
						$this->errorCode = self::ERROR_PASSWORD_INVALID;
					}elseif(!$this->checkIp($cmpUserid->zm_id)){
						//验证IP
						$this->errorCode = self::ERROR_PASSWORD_INVALID;
					}else{
						Yii::app()->user->setState('usertype','sub_member');
						Yii::app()->user->setState('cmpid',$cmpUserid->zm_id);
						Yii::app()->user->setState('subid',$memSub->zms_id);
						Yii::app()->user->setState('subname',$memSub->zms_username);
						//商家会员组
						$criteria=new CDbCriteria;
						$criteria->compare('zmg_id',3);
						$criteria->join = ' LEFT JOIN {{mem_glog}} mg ON mg.itemname=t.zmt_id ';
						$criteria->addCondition(' mg.userid ="'.$cmpUserid->zm_id.'" ');
						$companyGroup = MemType::model()->find($criteria);
						if(!empty($companyGroup)){
							$cGroup['group'] = $companyGroup->memgroup->attributes;
							$cGroup['type'] = $companyGroup->attributes;
							Yii::app()->user->setState('comgroup',$cGroup,1);
						}
						$this->errorCode = self::ERROR_NONE;
						//记录公司子账号ID，权限访问时使用
						$this->_id = $this->username;
					}
				}
			}
		}else{
			//会员登录
			$user = Member::model()->findByAttributes(array('zm_name' => $this->username));
			if(empty($user))//手机登录
				$user = Member::model()->findByAttributes(array('zm_mob' => $this->username));
			if(empty($user))//邮箱登录
				$user = Member::model()->findByAttributes(array('zm_email' => $this->username));
			if ($user === null) {
				$this->errorCode = self::ERROR_USERNAME_INVALID;
			} else {
				$this->username = $user->zm_name;
				if ($user->zm_psword !== $user->passwordEncode($this->password)) {
					$this->errorCode = self::ERROR_PASSWORD_INVALID;
				} else {
					Yii::app()->user->setState('usertype','member');
					$this->_id = $user->zm_id;
					if (!$user->zm_last_time) {
						$lastLogin = time();
					} else {
						$lastLogin = $user->zm_last_time;
					}
					//通过UserIdentity->setState保存的会话变量登录时将作为数组被设置到cookie（若开启）
					$this->setState('lastLoginTime', $lastLogin);
					$this->setState('loginTime', time());
					$this->errorCode = self::ERROR_NONE;
				}
			}
		}
		return!$this->errorCode;
	}
	
	public function getid(){
		return $this->_id;
	}
	
	/**
	 *商家子账户IP效验
	 */
	public function checkIp($cmpid){
		$block = Block::model()->findByAttributes(array('zbl_pos'=>'security_ip','zm_id'=>$cmpid));
		$ipdata = unserialize($block->zbl_data);
		$user_ip = Yii::app()->request->getUserHostAddress();
		if(in_array($user_ip,$ipdata)){
			return true;
		}else{
			return false;
		}
	}
}